//
//  STTabbarController.swift
//  STMusic
//
//  Created by stoneobs on 16/10/18.
//  Copyright © 2016年 stoneobs. All rights reserved.
//

import UIKit
class YYTabbarController: UITabBarController {
    
    var test:String?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        
        addNav(childVc: YYRecomendViewController(), title: "推荐", image: "tabbar_home_normal", selectedImage: "tabbar_home_selected");
        addNav(childVc: YYMusicArticalViewController(), title: "乐文", image: "tabbra_attention_normal", selectedImage: "tabbra_attention_selected");
        addNav(childVc: YYDiscoverViewController(), title: "发现", image: "tabbar_find2_normal", selectedImage: "tabbar_find2_selected");
        addNav(childVc: YYEspeciallyViewController(), title: "特别", image: "tabbar_especially_normal", selectedImage: "tabbar_especially_selected");


        let  tabbar = STTbabar();
        tabbar.showTopShodow();
        tabbar.barTintColor = YYThemeStyleManger.defultManger.gradualColor(frame: .init(x: 0, y: 0, width: SCREEN_WIDTH, height: 44));

        tabbar.didChosed2 = {
          // self.selectedIndex = 2;
        }
        
        setValue(tabbar, forKey: "tabBar");
    
    
    
        let musicView = YYDefaultMusicAnimationView.sharedInstance;
        YYDefaultMusicAnimationView.sharedInstance.startAnimation();
        musicView.centerY = 44;
        musicView.left = SCREEN_WIDTH - 55;
        musicView.clicAction = { view in
        
            STLayoutViewController.defaultLayout().showRightViewController(true);
        }
        self.view.addSubview(musicView);
    }

    func addNav(childVc:UIViewController,title:String,image:String ,selectedImage:String) -> Void {
        childVc.title = title;
        childVc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.flatBlack], for: UIControlState.selected);
        childVc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:ThirdTextColor!], for: UIControlState.normal);
        childVc.tabBarItem.image = UIImage.init(named: image);
        childVc.tabBarItem.selectedImage = UIImage.init(named: selectedImage)?.withRenderingMode(.alwaysOriginal);

        let nav = STNavigationController.init(rootViewController: childVc);
        nav.navigationBar.isTranslucent = false;
        //nav.navigationBar.shadowImage = UIImage.init(color: .white, size: .init(width: SCREEN_WIDTH, height: 0.5));
        self.findHairlineImageViewUnder(nav.navigationBar).isHidden = true;
        nav.navigationBar.barTintColor = YYThemeStyleManger.defultManger.gradualColor(frame: nav.navigationBar.bounds);
       

        addChildViewController(nav);
        
    }



}


class STTbabar: UITabBar {
  
    var didChosed2 :(Void)->() = {
        
    }
    var totalSteps: Int = 0{
        willSet {
            print("About to set totalSteps to \(newValue)")
        }
        didSet {
            if totalSteps > oldValue  {
                print("Added \(totalSteps - oldValue) steps")
            }
        }
    }
    
    override init(frame: CGRect) {

        
        super.init(frame: frame)
       
       
        
        
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func layoutSubviews() {
        super.layoutSubviews();
//        let but = UIButton.init(frame: CGRect.init(x: 0, y: -10, width: 50, height: 50));
//        but.setBackgroundImage(UIImage.init(named: "tabBar_send_click"), for: UIControlState.normal);
//        but.setBackgroundImage(UIImage.init(named: "tabBar_send_click"), for: UIControlState.selected);
//        but.titleEdgeInsets = UIEdgeInsets.init(top: 57, left: 0, bottom: 0, right: 0);
//        but.setTitle("IMusic", for: .normal);
//        but.titleLabel?.font = UIFont.systemFont(ofSize: 11);
//        //but.setTitleColor(.red, for: .selected);
//        but.setTitleColor(UIColor.init(hexString: "333333"), for: .normal);
//        let child = subviews[3];
//        but.centerX = SCREEN_WIDTH/2;
//        //but.centerY = height/2;
//        child.removeFromSuperview();
//        addSubview(but);
//        but.addTarget(self, action:#selector(middleSelected(sender:)), for: .touchUpInside);
        
        
    }
    func middleSelected(sender:UIButton) -> Void {
        sender.isSelected = !sender.isSelected;
        didChosed2();
    }
    

}
